package com.movisol.messengerprofileservice.controllers;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.movisol.messengerprofileservice.ProfileServiceManager;
import com.movisol.messengerprofileservice.data.Friend;
import com.movisol.messengerprofileservice.data.Friends;
import com.movisol.messengerprofileservice.data.UserProfile;
import com.movisol.messengerprofileservice.helpers.MessengerProfileHelper;
import com.movisol.messengerprofileservice.listeners.IFriendsDataLoadedListener;
import com.movisol.messengerprofileservice.listeners.IPushNotificationSubscribed;
import com.movisol.messengerprofileservice.listeners.IUserProfileDataLoadedListener;
import com.movisol.tools.HelperUtils;


public class ProfileController {
	
	private static ProfileController INSTANCE = null;
	private UserProfile userProfile;
	private UserProfile tmpUserProfile;
	private Friend tmpFriend;
	private List<String> emails;
	private Friends friends;
	private Context context;
	private ProfileServiceManager psm = null;
	
	private String conversationUriCacheFileName;
	
	//It does not allow to create a new instance of the class
	private ProfileController()
	{}

	
	public static ProfileController getInstance()
	{
		if( INSTANCE == null)
			INSTANCE =  new ProfileController();
		
		return INSTANCE;
	}
	
	
	//Blocks the clone method
	@Override
	protected Object clone() throws CloneNotSupportedException{
		throw new CloneNotSupportedException("Clone is not allowed");
	}
	
	
	public void initialize(Context context)
	{
		this.context = context;
		userProfile = new UserProfile();
		friends = new Friends();
		psm = new ProfileServiceManager(context);
	
	}
	
	public void initializeProfile(String email)
	{
		if(userProfile == null)
			userProfile = new UserProfile();
		
		userProfile.setEmail(email);
		String signature = MessengerProfileHelper.generateSignature(HelperUtils.getDeviceId(context)+HelperUtils.getConfigParam("ProfileSecretPhrase", context));
		userProfile.setSignature(signature);
	}
	
	public String updateUserFriends(String sku, String version, String languageId, String userId, Friends friends) {
		return psm.updateUserFriends(sku, version, languageId, userId, friends);
	}

	public String getUserFriends(String sku, String version, String languageId, String userId, String signature) {
		return psm.getUserFriends(sku, version, languageId, userId, signature);
	}


	public String saveUserProfile(UserProfile tmpUserProfile) {
		return psm.saveUserProfile(tmpUserProfile);
	}
	

	public String subscribePushNotification(String sku, String version, String languageId, String userId, String androidMedium,
			String registrationId) {
		return psm.subscribeNotificationPush(sku, version, languageId, userId, androidMedium, registrationId);
	}

	
	
	/******************UserProfileDataLoaded*****************/
	public void addUserProfileDataLoadedListener(IUserProfileDataLoadedListener listener){
		if(psm != null)
			psm.addUserProfileDataLoadedListener(listener);
	}
	
	public void removeUserProfileDataLoadedListener(IUserProfileDataLoadedListener listener){
		if(psm != null)
			psm.removeUserProfileDataLoadedListener(listener);
	}
	
	public void fireUserProfileDataLoadedEvent(String error) {
		if(psm != null)
			psm.fireUserProfileDataLoadedEvent(error);
	}
	
	/********************************************************/
	
	
	
	/********************FriendsDataLoaded******************/
	public void addFriendsDataLoadedListener(IFriendsDataLoadedListener listener){
		if(psm != null)
			psm.addFriendsDataLoadedListener(listener);
	}
	
	
	public void removeFriendsDataLoadedListener(IFriendsDataLoadedListener listener){
		if(psm != null)
			psm.removeFriendsDataLoadedListener(listener);
	}
	
	
	public void fireFriendsDataLoadedEvent(String error) {
		if(psm != null)
			psm.fireFriendsDataLoadedEvent(error);
		
	}
	/********************************************************/
	
	
		
	/********************FriendsDataUpdated*****************/
	public void addFriendsDataUpdatedListener(IFriendsDataLoadedListener listener){
		if(psm != null)
			psm.addFriendsDataUpdatedListener(listener);
	}
	
	
	public void removeFriendsDataUpdatedListener(IFriendsDataLoadedListener listener){
		if(psm != null)
			psm.removeFriendsDataUpdatedListener(listener);
	}
	
	
	public void fireFriendsDataUpdatedEvent(String error) {
		if(psm != null)
			psm.fireFriendsDataUpdatedEvent(error);
	}
	/********************************************************/
	
	
	/********************PushNotificationSubscribed*****************/
	public void addPushNotificationSubscribedListener(IPushNotificationSubscribed listener){
		if(psm != null)
			psm.addPushNotificationSubscribedListener(listener);
	}
	
	
	public void removePushNotificationSubscribedListener(IPushNotificationSubscribed listener){
		if(psm != null)
			psm.removePushNotificationSubscribedListener(listener);
	}
	
	
	public void firePushNotificationSubscribedEvent(String error) {
		if(psm != null)
			psm.firePushNotificationSubscribedEvent(error);
	}
	/********************************************************/

	
	
	public Friends fillFriendsObjectWithEmails(List<String> emailList, Integer status, String userId) {
		Friends friends = new Friends();
		friends.setFriendList(new ArrayList<Friend>());
		
		String signature = MessengerProfileHelper.generateSignature(getUserProfile().getUserId()+HelperUtils.getConfigParam("ProfileSecretPhrase", context));
		friends.setSignature(signature);
		
		
		for(int i = 0; i < emailList.size(); i++)
		{
			UserProfile userProfile = new UserProfile();
			userProfile.setEmail(emailList.get(i));
			userProfile.setUserId(userId);
			
			friends.getFriendList().add(new Friend());
			friends.getFriendList().get(i).setUserProfile(userProfile);
			friends.getFriendList().get(i).setFriendStatusId(status);
			
		}
		
		return friends;
		
	}
	
	public UserProfile getTmpUserProfile() {
		return tmpUserProfile;
	}


	public void setTmpUserProfile(UserProfile tmpUserProfile) {
		this.tmpUserProfile = tmpUserProfile;
	}
	
	public UserProfile getUserProfile() {
		return userProfile;
	}

	
	public void setUserProfile(UserProfile userProfile) {
		this.userProfile = userProfile;
	}

	public Friends getFriends() {
		return friends;
	}

	public void setFriends(Friends friends) {
		this.friends = friends;
	}
	
	
	public Friend getTmpFriend() {
		return tmpFriend;
	}


	public void setTmpFriend(Friend tmpFriend) {
		this.tmpFriend = tmpFriend;
	}

	public Context getContext() {
		return context;
	}

	public List<String> getEmails() {
		return emails;
	}


	public void setEmails(List<String> emails) {
		this.emails = emails;
	}
	
	public String getConversationUriCacheFileName() {
		return conversationUriCacheFileName;
	}


	public void setConversationUriCacheFileName(String conversationUriCacheFileName) {
		this.conversationUriCacheFileName = conversationUriCacheFileName;
	}

	
	
}
